#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#include <public/globalsensor_tc_store.h>
#include <public/PackMemManager.h>
#include <public/sgi_link_layer.h>
#include <public/ssi_link_layer.h>

#define MAX_BYTES_FROM_FILE 30
#define MIN_BYTES_FROM_FILE 1
#define MAX_SENSOR 20



uint64_t fileSize( const char* filePath ){

	uint64_t fsize = 0;
	std::ifstream file( filePath, std::ios::binary );

	fsize = file.tellg();
	file.seekg( 0, std::ios::end );
	fsize = (uint64_t)file.tellg() - fsize;
	file.close();

	return fsize;
}




int main() {

	cout << "!!!Hello World this is me!!!" << endl; // prints !!!Hello World!!!


	//Global pack Pool
	PackPool packPool;
	CGlobalSensorTCStore tcStore;

	CPackHandler m_currentPack;

	CPackListHandler free_pack_list;
	//CPackListHandler m_sensorTCPackList[255];
	//uint16_t m_sensorID[255];
	uint16_t pack_num;
	uint32_t total_pack_num=0;


	packPool.InitPackagesMemory();




	//Utilizar

	//************************************************************************
	//Test 1 add random
	//************************************************************************

	do{

		pack_num=rand()%100;

		packPool.AllocPackages(pack_num,free_pack_list);
		total_pack_num+=pack_num;

		tcStore.add_free_pack_list(free_pack_list);

	}while(!packPool.IsEmpty());


	tcStore.get_free_pack_list(free_pack_list);

	if(free_pack_list.CheckPackCounter()!=total_pack_num){

		cout << "!!!Error en add_free_pack_list y get_free_pacpocket hola caracolak_list!!!" << endl; // prints !!!Hello World!!!

	}

	tcStore.add_free_pack_list(free_pack_list);

	//***********************************************************************
	//Test 2  random store_ssi_tm_frame
	//************************************************************************

	char * image[] = {
			"./test_files/tranquilidad_copy.jpeg",
			"./test_files/tranquilidad_copy2.jpeg",
			"./test_files/tranquilidad_copy3.jpeg",
			"./test_files/tranquilidad_copy4.jpeg",
			"./test_files/tranquilidad_copy5.jpeg",
			"./test_files/tranquilidad_copy6.jpeg",
			"./test_files/tranquilidad_copy7.jpeg",
			"./test_files/tranquilidad_copy8.jpeg",
			"./test_files/tranquilidad_copy9.jpeg",
			"./test_files/tranquilidad_copy10.jpeg",
			"./test_files/tranquilidad_copy11.jpeg",
			"./test_files/tranquilidad_copy12.jpeg",
			"./test_files/tranquilidad_copy13.jpeg",
			"./test_files/tranquilidad_copy14.jpeg",
			"./test_files/tranquilidad_copy15.jpeg",
			"./test_files/tranquilidad_copy16.jpeg",
			"./test_files/tranquilidad_copy17.jpeg",
			"./test_files/tranquilidad_copy18.jpeg",
			"./test_files/tranquilidad_copy19.jpeg",
			"./test_files/tranquilidad_copy20.jpeg",
			"./test_files/tranquilidad_copy21.jpeg",
			"./test_files/tranquilidad_copy22.jpeg",
			"./test_files/tranquilidad_copy23.jpeg",
			"./test_files/tranquilidad_copy24.jpeg",
			"./test_files/tranquilidad_copy25.jpeg",
			"./test_files/tranquilidad_copy26.jpeg",
			"./test_files/tranquilidad_copy27.jpeg",
			"./test_files/tranquilidad_copy28.jpeg",
			"./test_files/tranquilidad_copy29.jpeg",
			"./test_files/tranquilidad_copy30.jpeg",
			"./test_files/tranquilidad_copy31.jpeg",
			"./test_files/tranquilidad_copy32.jpeg",
			"./test_files/tranquilidad_copy33.jpeg",
			"./test_files/tranquilidad_copy34.jpeg",
			"./test_files/tranquilidad_copy35.jpeg",
			"./test_files/tranquilidad_copy36.jpeg",
			"./test_files/tranquilidad_copy37.jpeg",
		    "./test_files/tranquilidad_copy38.jpeg",
			"./test_files/tranquilidad_copy39.jpeg",
			"./test_files/tranquilidad_copy40.jpeg",
			"./test_files/tranquilidad_copy41.jpeg",
			"./test_files/tranquilidad_copy42.jpeg",
			"./test_files/tranquilidad_copy43.jpeg",
			"./test_files/tranquilidad_copy44.jpeg",
	};

	uint64_t filesize,pendingBytes;
	byte_t nextFrameBytes;
	sgi_link_layer_sen_tc_frame_t frame;
	int16_t FRAME;


	for (int z=0; z<MAX_SENSOR;z++){
		filesize=fileSize("./test_files/tranquilidad.jpeg" );
		pendingBytes=filesize;
		ifstream file("./test_files/tranquilidad.jpeg", ios::binary);
		FRAME=z;
		while(pendingBytes){
			if(pendingBytes>MAX_BYTES_FROM_FILE){
				nextFrameBytes = 1 + rand()%MAX_BYTES_FROM_FILE;
			}else
				nextFrameBytes=pendingBytes;
			frame.sensor_id= FRAME;
			frame.length=nextFrameBytes;
			for (int i=0;i<frame.length;i++){
				byte_t aux;
				aux=file.get();
				frame.data[i]=aux;
			}
			tcStore.store_sen_tc_frame(frame);
			pendingBytes-=nextFrameBytes;
		}
		file.close();
	}


	cout<<"--------------------------------"<<endl;

	ssi_link_layer_data_frame_info_t ssiFrame;
	for (int sensor=0; sensor<MAX_SENSOR; sensor++){
		ofstream file_salida(image[sensor], ios::binary);
		cout<<image[sensor]<<endl;
		while(!tcStore.m_sensorTCPackList[sensor].IsEmpty()){
			tcStore.get_next_ssi_sen_tc_frame_info(sensor,ssiFrame);
			for (int i=0; i<ssiFrame.length;i++){
				file_salida.put(ssiFrame.data[i]);
			}
			tcStore.delete_packed_tc_head(sensor);
		}
		file_salida.close();
	}
	return 0;
}


